package org.jrfe.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

	

	public static List<String> readFile(String path) throws IOException {

		List<String> list = new ArrayList<String>();

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			BufferedReader input = new BufferedReader(
					new InputStreamReader(
					FileUtil.class.getClassLoader().getResourceAsStream(path), "UTF8"));
			try {
				String line = null; // not declared within while loop
				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					list.add(line);
				}
			} finally {
				input.close();
			}
			
		} catch (IOException ex) {
			throw ex;
		}

		return list;

	}

	
	public static List<String> readFile(File file) throws IOException {

		List<String> list = new ArrayList<String>();

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));
			try {
				String line = null; // not declared within while loop
				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					list.add(line);
				}
			} finally {
				input.close();
			}
			
		} catch (IOException ex) {
			throw ex;
		}

		return list;

	}

}
